/**
 * Created by z on 2016/9/8.
 * 查看整改记录 - 维修整改 - 腐蚀控制
 */

;(function ($, window, document, undefined) {

    // 整改记录编号
    var rid = $('#JQ_repairTaskId').val();
    // 记录数据列表
    var $datatable_details = $('#JQ_datatable_details');
    // 数据列表对象
    var DTRECORD;
    // 查询对象
    var filter = {
        getSearch: function(param){
            var data = {};
            data.mode = true;
            if(param){
                if (param.start == 0) {
                    data['jsonBean.pageNum'] = 1;
                } else {
                    data['jsonBean.pageNum'] = param.start / 10 + 1;
                }
                data['jsonBean.pageCount'] = param.length;
            }
            data.repairTaskId = function(){
                var v = $('#JQ_repairTaskId').val();
                if(v){
                    return v;
                }
                return '';
            }();
            return data;
        }
    };

    // 加载列表
    if (rid) {
        DTRECORD = $datatable_details.DataTable({
            'language': {
                'paginate': {
                    'next': '下一页',
                    'previous': '上一页'
                },
                'info': '本次检索共 _TOTAL_ 条，每页显示 10 条，共 _PAGES_ 页',
                'zeroRecords': DyyBase.table_empty(),
                'infoEmpty': ''
            },
            'autoWidth': false,
            'order': [],
            'serverSide': true,
            'pageLength': 10,
            'ordering': false,
            'processing': false,
            'searching': false,
            'lengthChange': false,
            'ajax': function (data, callback) {
                var param = filter.getSearch(data);
                DyyBase.ajax(corrctrlApi.url_cath_details(), param, function (dataset) {
                    var returnData = {};
                    returnData.draw = data.draw;
                    returnData.recordsTotal = dataset.result.defectInfoList.total;
                    returnData.recordsFiltered = dataset.result.defectInfoList.total;
                    returnData.data = DyyBase.clearNull(dataset.result.defectInfoList.list);
                    callback(returnData);

                    // 项目信息
                    $('#JQ_pipe_name').text(dataset.result.repairTask.pipeLineName);
                    $('#JQ_begin_name').text(dataset.result.repairTask.fromPointName);
                    $('#JQ_end_name').text(dataset.result.repairTask.toPointName);
                    $('#JQ_project_name').text(dataset.result.repairTask.name);
                    $('#JQ_company_name').text(dataset.result.repairTask.assemblyCompany);
                    $('#JQ_include').text(dataset.result.includeTestStation);
                });
            },
            'drawCallback': function () {
            },
            'columns': [
                {
                    data: 'pipeLineName', render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
                },
                {
                    data: 'pipeValveroomName', render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
                },
                {
                    data: 'eqptType_label', render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
                },
                {
                    data: 'eqptNumber', render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
                },
                {
                    data: 'mileage2', render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
                },
                {
                    data: 'repairSuggestion_label', render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
                },
                {
                    data: 'lastRepairDate', render: function (data) {
                    var time = Dyy.getDate(data);
                    return '<span title="' + data + '">' + time + '</span>';
                }
                },
                {
                    data: 'id', render: function (data, type, row) {
                    if(row.rectificationDescription === '-'){
                        return '<i title="暂无记录" class="zy-record-no-icon" data-id="' + data + '"></i>';
                    }
                    return '<i title="查看记录" class="zy-record-icon" data-id="' + data + '"></i>';
                }
                },
                {
                    data: 'id', render: function (data) {
                    return '<i title="展开详情" class="zy-details-icon" data-id="' + data + '"></i>';
                }
                }
            ]
        });
    }

    // 详情模板
    var recordTemplate = [
        {name: 'pipeLineName', title: '管道名称：'},
        {name: 'pipeValveroomName', title: '所属场站阀室：'},
        {name: 'eqptType_label', title: '设备类型：'},
        {name: 'eqptNumber', title: '设备编号：'},
        {name: 'mileage2', title: '绝对距离(m)：'},
        {name: 'nagmName', title: '最近测试桩：'},
        {name: 'nagmMileage', title: '相对最近测试桩距离(m)：', render: function(d){
            if(d === '-'){
                return d;
            }else{
                return parseFloat(d) / 1000;
            }
        }},
        {name: 'description', title: '异常情况：'},
        {name: 'repairSuggestion_label', title: '修复建议：'},
        {name: 'lastRepairDate', title: '最迟维修时间：', render: function(d){
            return  Dyy.getDate(d);
        }},
        {name: 'testingCompany', title: '检测单位：'},
        {name: 'testingDate', title: '检测日期：', render: function(d){
            return  Dyy.getDate(d);
        }},
        {name: 'remarks', title: '备注：'}
    ];

    // 整改记录
    var repairTemplate = [
        {name: 'description', title: '异常情况：'},
        {name: 'rectificationDate', title: '整改时间：', render: function(d){
            return  Dyy.getDate(d);
        }},
        {name: 'rectificationDescription', title: '整改记录：'},
        {name: 'rectificationCompany', title: '整改单位：'},
        {name: 'remarks', title: '备注：'}
    ];

    // 展开详情
    $datatable_details.on('click', 'i.zy-details-icon', function () {
        DyyBase.showDetails($(this), DTRECORD, $datatable_details, recordTemplate);
    });

    // 展开整改记录
    $datatable_details.on('click', 'i.zy-record-icon', function () {
        //DyyBase.showDetails($(this), DTRECORD, $datatable_details, repairTemplate);
        var $this = $(this);
        var id = $this.attr('data-id');
        var $exist = $datatable_details.find('tr[data-rid="'+id+'"]');
        $datatable_details.find('i.zy-record-icon').removeClass('active');
        $datatable_details.find('i.zy-details-icon').removeClass('active');
        $datatable_details.find('tr.zy-dy-tr').hide();
        $this.addClass('active');
        if($exist.length > 0){
            $exist.show();
        }else{
            var $tr = $this.parent('td').parent('tr');
            var data = DTRECORD.row($tr).data();
            var $tr_dy = $('<tr class="zy-dy-tr" data-rid="' + id + '"><td colspan="9"><div class="zy-details-box"><div class="zy-dy-inner"><div class="zy-dy-p"></div></div></div></div></td></tr>');
            var $inner = $tr_dy.find('div.zy-dy-inner');
            var $p_dy = $tr_dy.find('div.zy-dy-p');
            var $footer = $('<div class="zy-dy-footer"><button type="button" class="zy-button zy-pick-up" data-id="' + id + '"><i></i>收起</button></div>');
            var $btn = $footer.find('button.zy-pick-up');
            $btn.click(function(){
                $this.removeClass('active');
                $datatable_details.find('tr[data-rid="'+id+'"]').hide();
            });
            var p = '';
            for(var i = 0, len = repairTemplate.length; i < len; i++){
                if(data.hasOwnProperty(repairTemplate[i]['name'])){
                    if(repairTemplate[i]['render']){
                        var temp = repairTemplate[i]['render'](data[repairTemplate[i]['name']]);
                        p += '<p>'+repairTemplate[i]['title']+'<span title="'+temp+'">'+temp+'</span></p>';
                    }else{
                        p += '<p>'+repairTemplate[i]['title']+'<span title="'+data[repairTemplate[i]['name']]+'">'+data[repairTemplate[i]['name']]+'</span></p>';
                    }
                }
            }
            $p_dy.html(p);
            $inner.append($p_dy).append($footer);
            $tr.after($tr_dy);
        }
    });

    // 返回
    $('#JQ_go_list').click(function () {
        window.location.href = DyyBase.root_path + '/corrctrl/CathRepairTask/init.html';
    });

    // 去查看整改效果
    $('#JQ_view_effect').click(function(){
        var $this = $(this);
        var bool = $('#JQ_include').text();
        if(bool == 'true'){
            var url = DyyBase.root_path + '/corrctrl/CathRepairTask/cathEffect.html?repairTaskId=' + rid;
            window.location.href = url;
        }else{
            $this.MSG({
                content: '本次阴保整改不包括测试桩,无法提供整改效果对比!'
            });
        }
    });

})(jQuery, window, document);